import dayjs from 'dayjs';
import {
    commentPage
} from '../../../api/goodsdetail';
Page({
    data: {
        commentList: [],
        pageIndex: 1,
        pageSize: 10,
        total: 0,
        loadMoreStatus: 4,
        spuId: '',
        commentType: 0,
    },
    onLoad(options) {
        const {
            spuId
        } = options;
        this.setData({
            spuId: spuId
        }, () => {
            this.getComments();
        })
    },
    async getComments() {
        const {
            pageIndex,
            pageSize,
            spuId,
            commentType
        } = this.data;
        this.setData({
            loadMoreStatus: 1,
        });
        const res = await commentPage(spuId, commentType, pageIndex, pageSize);
        let list = res.list
        list.map(item => {
            item.createTime = dayjs(item.createTime).format('YYYY-MM-DD')
        })
        let status = 0
        if (pageIndex * pageSize < res.total) {
            status = 2
        }
        setTimeout(() => {
            let commentList = this.data.commentList == 1 ? list : [...this.data.commentList, ...list]
            this.setData({
                commentList: commentList,
                total: res.total,
                loadMoreStatus: status
            });
        }, 200)
    },
    changeTag(e) {
        const {
            type
        } = e.currentTarget.dataset;
        if (type != this.data.commentType) {
            this.setData({
                commentList: [],
                total: 0,
                pageIndex: 1,
                commentType: type
            })
            this.getComments();
        }
    },
    onReachBottom() {
        const {
            total,
            pageSize,
            pageIndex
        } = this.data;
        if (pageIndex * pageSize < total) {
            this.setData({
                pageIndex: this.data.pageIndex + 1
            });
            this.getComments();
        }
    },
});